//
//  TestFormTableVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/12/9.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import CooperSwift
import RxSwift

class InputTitleCellModel: NSObject, FormModelProtocol, PublicTableCellConfigProtocol  {
    
    typealias TargetType = [UIView]
    
    var model: Any?
    
    required init(_ model: Any?) {
        self.model = model
    }
    
    func cellCls() -> UITableViewCell.Type {
        return InputTitleCell.self
    }
    
    func once(_ target: [UIView], cell: UITableViewCell) {
        
    }
    
    func every(_ target: [UIView], cell: UITableViewCell) {
        
    }
    
}

class TestFormTableVM: NSObject, TableViewModelProtocol, UITableViewDelegate, FormResultProtocol {
    
    let bag = DisposeBag()
    
    func initDown() {
        
        let formCellConfig0 = TestFormStackCellModel("1").then {
            $0.cellHeight = 54
            $0.dataKey = "testKey0"
        }
        
        ///
        let formCellConfig1 = InputTitleCellModel("1").then {
            $0.rules.append([.maxLength(8), .money])
            $0.cellHeight = 54
            $0.dataKey = "testKey1"
        }
        
        /// webview
        let webConfig0 = webUrlRow("https://www.baidu.com", key: bindKey(bag))
        
        let webConfig1 = webContentRow("https://www.baidu.com", key: bindKey(bag))
        
        let lb0 = lbRow("https://www.baidu.com", bgColor: .random(), padding: UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)).then {
            $0.bgColor = .random()
        }
        
        let btnControl0 = btnConfig("测试按钮1").then {
            $0.frame = CGRect(20, 0, (Screen.width - 60) / 2.0, 44)
            $0.bgColor = .random()
            $0.method = "tap0"
        }
        let btnControl1 = btnConfig("测试按钮2").then {
            $0.frame = CGRect((Screen.width - 60) / 2.0 + 40, 0, (Screen.width - 60) / 2.0 , 44)
            $0.bgColor = .random()
            $0.method = "tap1"
        }
        
        let btn0 = btnsRow([btnControl0, btnControl1], cellHeight: 44, key: bindKey(bag), isTarget: true)
        
        let singleImageShow = imageShowRow(imageConfig("https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/320.jpg"), key: bindKey(bag))
        
        var imagesArray: [TableShowImageConfig] = []
        for index in 0...7 {
            let left: CGFloat = index % 3 == 0 ? 12 : 8
            let width = (Screen.width - 32.0 - 8)/3.0 - 0.01
            let config = imagesConfig("https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(320 + index).jpg", vLeftCons: left, vWidthCons: width)
            imagesArray.append(config)
        }
        let imageViewsRow = imagesShowByMultImageViewRow(imagesArray, key: bindKey(bag))
        
        var items = Array<CollectionCellConfigProtocol>()
        for i in 0...4 {
            let urlBTModel = TableShowBaseControlConfig().then {
                $0.url = "https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(i+320).jpg"
            }
            let urlModel = TestShowCollectImageCVCModel.init(urlBTModel)
            items.append(urlModel)
        }
        let imageCollectionRow = imagesShowByCollectionViewRow(items, cellHeight: 328, key: bindKey(bag))
        
        addFirstSectionItems([
            formCellConfig0,
            formCellConfig1,
//            singleImageShow,
            imageViewsRow,
            imageCollectionRow,
//            webConfig0,
//            webConfig1,
            lb0,
            lineRow(12, color: .clear),
            btn0,
            lineRow(12, color: .clear),
            btnRow("测试按钮3", color: .darkGray, bgColor: .red, frame: .init(20, 0, Screen.width - 40, 44), cellHeight: 44, key: bindKey(), isTarget: true)
        ])
        
    }
    
    @objc func btnTap(_ info: TableShowBtnConfig) {
        debugPrint("\(info.method ?? "") --> \(info.text ?? "")")
        debugPrint(currentParams())
    }
    
    @objc func tap0(_ info: TableShowBtnConfig) {
        debugPrint("\(info.method ?? "") --> \(info.text ?? "")")
        debugPrint(currentParams())
    }
    
    @objc func tap1(_ info: TableShowBtnConfig) {
        debugPrint("\(info.method ?? "") --> \(info.text ?? "")")
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return vm_tableView(tableView, heightForRowAt: indexPath)
    }
    
}


class TestFormTableVC: UIViewController, TableEnableProtocol {
    
    let vm = ViewModelFactory.tableVM(TestFormTableVM.self)
    let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configAll(vm, disposeBag: bag, edges: .zero)
        
    }
    
}
